package com.mbyte.easy.taskstudent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mbyte.easy.qcbyclass.entity.QcbyClass;
import com.mbyte.easy.qcbyclass.service.IQcbyClassService;
import com.mbyte.easy.security.entity.SysUser;
import com.mbyte.easy.security.service.IUserService;
import com.mbyte.easy.status.entity.Status;
import com.mbyte.easy.status.service.IStatusService;
import com.mbyte.easy.student.entity.Student;
import com.mbyte.easy.student.service.IStudentService;
import com.mbyte.easy.studentInfo.entity.TStudent;
import com.mbyte.easy.taskstudent.entity.TaskStudent;
import com.mbyte.easy.taskstudent.mapper.TaskStudentMapper;
import com.mbyte.easy.taskstudent.service.TaskIStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lp
 * @since 2020-06-17
 */
@Service
public class TaskStudentServiceImpl extends ServiceImpl<TaskStudentMapper, TaskStudent> implements TaskIStudentService {

    @Autowired
    private TaskIStudentService taskIStudentService;
    @Autowired
    private IQcbyClassService qcbyClassService;
    @Autowired
    private IStatusService statusService;
    @Autowired
    private IUserService userService;
    @Override
    public IPage<TaskStudent> getByClassId(Page<TaskStudent> userPage, QueryWrapper<TaskStudent> queryWrapper, Long classId) {
        IPage<TaskStudent> page = page(userPage,queryWrapper);
        page.setRecords(null);
        List<TaskStudent> records = new ArrayList<>();
        List<Status> statusList = statusService.list();
        List<TaskStudent> studentList = taskIStudentService.list(new LambdaQueryWrapper<TaskStudent>().eq(TaskStudent::getClassId , classId));
        System.err.println(studentList);
        QcbyClass qcbyClass = qcbyClassService.getById(classId);
        String className = qcbyClass.getClassName();
        for (TaskStudent student : studentList) {
            SysUser user = userService.selectByPrimaryKey(student.getUserId());
            student.setUserName(user.getUsername());
            student.setClassName(className);
            for (int i1 = 0; i1 < statusList.size(); i1++) {
                if(student.getSex() == statusList.get(i1).getId()){
                    student.setStatusName(statusList.get(i1).getStatus());
                }
            }
            records.add(student);
        }
        page.setRecords(records);
        return page;
    }

    @Override
    public IPage<TaskStudent> pageInfo(Page<TaskStudent> page, QueryWrapper<TaskStudent> queryWrapper, TaskStudent taskstudent) {
        IPage<TaskStudent> page1 = page(page, queryWrapper);
        List<TaskStudent> records = page1.getRecords();
        List<TaskStudent> records1 = new ArrayList<>();
        List<Status> statusList = statusService.list();
        for (int i = 0; i < records.size(); i++) {
            TaskStudent taskStudent = records.get(i);
            for (int i1 = 0; i1 < statusList.size(); i1++) {
                if(taskStudent.getSex() == statusList.get(i1).getId()){
                    taskStudent.setStatusName(statusList.get(i1).getStatus());
                }
            }
            records1.add(taskStudent);
        }
        page1.setRecords(records1);
        return page1;
    }
}
